/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sg.edu.nus.iss.pacman.form;

import java.util.Date;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import sg.edu.nus.iss.pacman.dao.UserProfile;
import sg.edu.nus.iss.pacman.midlet.PacManMIDlet;

/**
 *
 * @author daniel
 */
public class EnterUserProfileForm extends Form implements CommandListener
{

    private Command cmdOK, cmdExit;
    private TextField name, email;
    private DateField dob;
    private ChoiceGroup gender;
    private String[] genderTypes =
    {
        "Male", "Female"
    };
    private static final String TITLE = "Enter User Profile";

    private PacManMIDlet mainApp;

    public EnterUserProfileForm(PacManMIDlet mainApp)
    {
        super(TITLE);
        this.mainApp = mainApp;
        init();
    }

    private void init()
    {
        name = new TextField("Name:", null, 15, TextField.ANY);
        email = new TextField("Email:", null, 30, TextField.EMAILADDR);
        dob = new DateField("Date of Birth:", DateField.DATE);
        gender = new ChoiceGroup("Gender: ", Choice.EXCLUSIVE, genderTypes, null);
        append(name);
        append(email);
        append(dob);        
        append(gender);
        cmdOK = new Command(PacManMIDlet.LABEL_CMD_OK, Command.OK, 1);
        addCommand(cmdOK);        
        cmdExit = new Command(PacManMIDlet.LABEL_CMD_EXIT, Command.EXIT, 1);
        addCommand(cmdExit);
        setCommandListener(this);

    }

    public void commandAction(Command c, Displayable d)
    {
        if(cmdOK == c && ! name.getString().trim().equals("") &&
        		! email.getString().trim().equals("") && dob.getDate() != null )
        {	
        	UserProfile userProfile = new UserProfile();
        	userProfile.setName(name.getString());        	
        	userProfile.setEmail(email.getString());
        	userProfile.setDob(dob.getDate());
        	userProfile.setGender(gender.getSelectedIndex() + 1);
            mainApp.saveScore(userProfile);
        }
        else if(cmdExit == c)
        {
            mainApp.displayMainMenu();
        } else {
        	mainApp.displayAlertError("Name, Email and Date of birth cannot empty");
        }
    }
}
